package com.YxyBackMange.Service.Ware.Impl;

import com.YxyBackMange.Entity.Ware.WareGoodsAttr;
import com.YxyBackMange.Entity.Ware.WareGoodsDetails;
import com.YxyBackMange.Entity.Ware.WareGoodsImg;
import com.YxyBackMange.Entity.Ware.WareGoodsInfo;
import com.YxyBackMange.Mapper.Ware.WareGoodsAttrMapper;
import com.YxyBackMange.Mapper.Ware.WareGoodsDetailsMapper;
import com.YxyBackMange.Mapper.Ware.WareGoodsImgMapper;
import com.YxyBackMange.Mapper.Ware.WareGoodsInfoMapper;
import com.YxyBackMange.Service.Ware.WareGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WareGoodsInfoServiceImpl implements WareGoodsInfoService{
	
	@Autowired
    private WareGoodsInfoMapper wareGoodsInfoMapper;
    @Autowired
    private WareGoodsDetailsMapper wareGoodsDetailsMapper;

    @Autowired
    private WareGoodsAttrMapper wareGoodsAttrMapper;
    @Autowired
    private WareGoodsImgMapper wareGoodsImgMapper;

    @Override
   public WareGoodsInfo insertService(WareGoodsInfo wareGoodsInfo){
        wareGoodsDetailsMapper.insertSelective(wareGoodsInfo.getGoodsDetails());
        wareGoodsInfo.setCreateTime(new Date());
        wareGoodsInfo.setDetailsId(wareGoodsInfo.getGoodsDetails().getId());
        wareGoodsInfoMapper.insertSelective(wareGoodsInfo);
        for (WareGoodsAttr wareGoodsAttr : wareGoodsInfo.getWareGoodsAttrList()) {
            wareGoodsAttr.setGoodsId(wareGoodsInfo.getId());
            wareGoodsAttrMapper.insertSelective(wareGoodsAttr);
        }
        for (WareGoodsImg wareGoodsImg : wareGoodsInfo.getGoodsImgList()) {
            wareGoodsImg.setGoodsId(wareGoodsInfo.getId());
            wareGoodsImgMapper.insertSelective(wareGoodsImg);
        }

        return wareGoodsInfo;
	   
   };
   @Override
   public WareGoodsInfo updateService(WareGoodsInfo wareGoodsInfo){
       wareGoodsInfoMapper.updateByPrimaryKeySelective(wareGoodsInfo);//更新商品信息
       WareGoodsAttr w = new WareGoodsAttr();
       w.setGoodsId(wareGoodsInfo.getId());
       wareGoodsAttrMapper.delete(w);//删除商品属性
       WareGoodsImg wi = new WareGoodsImg(wareGoodsInfo.getId());
       w.setGoodsId(wareGoodsInfo.getId());
       wareGoodsImgMapper.delete(wi);//删除商品图片
       for (WareGoodsAttr wareGoodsAttr : wareGoodsInfo.getWareGoodsAttrList()) {
           wareGoodsAttr.setGoodsId(wareGoodsInfo.getId());
           wareGoodsAttrMapper.insertSelective(wareGoodsAttr);//插入商品属性
       }
       for (WareGoodsImg wareGoodsImg : wareGoodsInfo.getGoodsImgList()) {
           wareGoodsImg.setGoodsId(wareGoodsInfo.getId());
           wareGoodsImgMapper.insertSelective(wareGoodsImg);//商品图片
       }
       WareGoodsInfo wareGoodsInfo1 = wareGoodsInfoMapper.selectByPrimaryKey(wareGoodsInfo);
       wareGoodsInfo.getGoodsDetails().setId(wareGoodsInfo1.getDetailsId());
       wareGoodsDetailsMapper.updateByPrimaryKeySelective(wareGoodsInfo.getGoodsDetails());
        return wareGoodsInfo;
   };
    @Override
    public WareGoodsInfo update(WareGoodsInfo wareGoodsInfo) {
        wareGoodsInfoMapper.updateByPrimaryKeySelective(wareGoodsInfo);
        return wareGoodsInfo;
    }
   @Override
   public WareGoodsInfo delete(WareGoodsInfo wareGoodsInfo){

	     wareGoodsInfoMapper.delete(wareGoodsInfo);
        return wareGoodsInfo;
   };
    @Override
    public WareGoodsInfo insert(WareGoodsInfo wareGoodsInfo){

        wareGoodsInfoMapper.insertSelective(wareGoodsInfo);
        return wareGoodsInfo;

        };
}
